package com.test.register;


import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @Aurher: QiaoHang
 * @Description:
 * @Data: 2019/9/13 10:34
 * @Modified By:
 */
public class MyBeanDefineationRegister implements ImportBeanDefinitionRegistrar {
    /**
     *
     * @param annotationMetadata    当前类的注解元数据
     * @param beanDefinitionRegistry    bean定义注册如容器的类(需要注入ioc容器的bean都会经过beanDefinitionRegistry进行注册)
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        boolean containGreen = beanDefinitionRegistry.containsBeanDefinition("com.test.pojo.Green");
        boolean containRed = beanDefinitionRegistry.containsBeanDefinition("red");
        if(containGreen && containRed){
            //如果容器包含Red 和 Green 实例，通过beandefinitionRegistory 注册一个Rainbow实例
            BeanDefinition beanDefinition=new RootBeanDefinition("com.test.pojo.RainBow");      //给出bean的定义信息
            beanDefinition.setScope("singleton");
            beanDefinition.setLazyInit(false);
            beanDefinitionRegistry.registerBeanDefinition("rainbow",beanDefinition);
        }
    }
}
